package com.github.wxpay.sdk;

import com.swetake.util.Qrcode;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import static com.github.wxpay.sdk.WXPayUtil.MD5;

public class Order {
    private String coder_url;
    private String out_trade_no = String.valueOf((long)(Math.random() * 100000000 + 98372819));

    public boolean setOrder(double money){
        MyConfig myConfig=new MyConfig();

        try {
            WXPay wxPay=new WXPay(myConfig);
            Map<String,String> map = new HashMap<>();
            map.put("body", "PetHub");   // 商品描述
            map.put("out_trade_no", out_trade_no); // 商户订单ID
            map.put("total_fee", String.valueOf((int)(money * 100)));//标价金额
            map.put("spbill_create_ip", "127.0.0.1");  //公网IP
            map.put("notify_url", "https://blog.csdn.net/qq_37126480");  // 通知地址 回调地址
            map.put("trade_type", "NATIVE");  // 交易类型

            Map<String, String> resultMap = wxPay.unifiedOrder(map);
            if(resultMap.get("result_code").equalsIgnoreCase("FAIL")){
                return false;
            }
            coder_url = resultMap.get("code_url");
            return true;//成功生成
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return false;
    }

    public String getOut_trade_no() {
        return out_trade_no;
    }

    public boolean getStatus(String orderID){
        try {
            String stringRandom = String.valueOf((int)(Math.random() * 100000));

            Map<String, String> dataMap = new HashMap<>();
            dataMap.put("appid", "wx8397f8696b538317");
            dataMap.put("mch_id", "1473426802");
            dataMap.put("out_trade_no", orderID);
            dataMap.put("nonce_str", stringRandom);

            String string = "appid=wx8397f8696b538317&mch_id=1473426802&out_trade_no=" + orderID + "&nonce_str=" + stringRandom + "&key=T6m9iK73b0kn9g5v426MKfHQH7X8rKwb";
            String sign = MD5(string).toUpperCase();
            dataMap.put("sign", sign);

            MyConfig wxPayConfig = new MyConfig();

            WXPay wxPay = new WXPay(wxPayConfig);
            Map<String, String> orderQuery = wxPay.orderQuery(dataMap);
            if(orderQuery.get("trade_state").equalsIgnoreCase("SUCCESS")){
                return true;
            }
            return false;
        }catch (Exception ex){
            System.out.println("getStatusException");
        }
        return false;
    }

    public String getCoder_url() {
        return coder_url;
    }

    private boolean getQRCode(String code_url) throws IOException {
        //计算二维码图片的高宽比
        // API文档规定计算图片宽高的方式 ，v是本次测试的版本号
        int v =6;
        int width = 67 + 12 * (v - 1);
        int height = 67 + 12 * (v - 1);

        Qrcode x = new Qrcode();

        x.setQrcodeErrorCorrect('L');
        x.setQrcodeEncodeMode('B');//注意版本信息 N代表数字 、A代表 a-z,A-Z、B代表 其他)
        x.setQrcodeVersion(v);//版本号  1-40
        String qrData = code_url;//内容信息

        byte[] d = qrData.getBytes("utf-8");//汉字转格式需要抛出异常

        //缓冲区
        BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);

        //绘图
        Graphics2D gs = bufferedImage.createGraphics();

        gs.setBackground(java.awt.Color.WHITE);
        gs.setColor(java.awt.Color.BLACK);
        gs.clearRect(0, 0, width, height);

        //偏移量
        int pixoff = 2;

        if (d.length > 0 && d.length < 120) {
            boolean[][] s = x.calQrcode(d);

            for (int i = 0; i < s.length; i++) {
                for (int j = 0; j < s.length; j++) {
                    if (s[j][i]) {
                        gs.fillRect(j * 3 + pixoff, i * 3 + pixoff, 3, 3);
                    }
                }
            }
        }
        gs.dispose();
        bufferedImage.flush();
        //设置图片格式，与输出的路径
        File file = new File("pic/qrcode.png");
        ImageIO.write(bufferedImage, "png", file );

        return true;
    }
}
